const crypto = require('crypto');
const fs = require('fs');
const path = require('path');


const publicKeyPath = path.join(__dirname, "../../key/public.pem");
function getPublicKeyPem(publicKeyPath) {
	const data = fs.readFileSync(publicKeyPath, 'utf-8');
	return data;
}
function verifySign(dataPath, signature) {
	//获取数据
	var dataToSign = fs.readFileSync(dataPath, 'utf-8');

	var md5 = crypto.createHash('md5');
	var md5data = md5.update(dataToSign).digest('hex');
	//获取公钥
	var publicKeyPem = getPublicKeyPem(publicKeyPath);
	var publicKey = crypto.createPublicKey({ key: publicKeyPem, format: 'pem', type: 'pkcs1' });
	// 验证签名
	var verify = crypto.createVerify('RSA-SHA256');
	verify.update(md5data);
	var isVerified = verify.verify(publicKey, signature, 'hex');
	return isVerified
}

module.exports = { verifySign }
